package Lib3d.Animation;


import javax.media.opengl.GL;

/**
 * A Bezier animation implementation. Note: this class is NOT ready!
 * 
 * @author Wassim Chegham
 * @version 0.1
 * @category Animation
 * 
 */
public class Bezier {

    // ////////////// Variables /////////////////////////

    // To switch the directin of an amimation.
    float animSign = 0.3f;

    // GLUnurbs theNurb;

    // /////////////// Functions /////////////////////////

    final int nbCtrlPoints = 25;

    final float s = 2f;

    float ctrlPoints[] = {

            -2f * this.s, -2f * this.s, 0f * this.s, -1f * this.s, -2f * this.s,
            0f * this.s, 0f * this.s, -2f * this.s, 0f * this.s, 1f * this.s,
            -2f * this.s, 0f * this.s, 2f * this.s, -2f * this.s, 0f * this.s,

            -2f * this.s, -1f * this.s, 0f * this.s, -1f * this.s, -1f * this.s,
            0f * this.s, 0f * this.s, -1f * this.s, 0f * this.s, 1f * this.s,
            -1f * this.s, 0f * this.s, 2f * this.s, -1f * this.s, 0f * this.s,

            -2f * this.s, 0f * this.s, 0f * this.s, -1f * this.s, 0f * this.s,
            0f * this.s, 0f * this.s, 0f * this.s, 0f * this.s, 1f * this.s, 0f * this.s,
            0f * this.s, 2f * this.s, 0f * this.s, 0f * this.s,

            -2f * this.s, +1f * this.s, 0f * this.s, -1f * this.s, +1f * this.s,
            0f * this.s, 0f * this.s, +1f * this.s, 0f * this.s, 1f * this.s,
            +1f * this.s, 0f * this.s, 2f * this.s, +1f * this.s, 0f * this.s,

            -2f * this.s, +2f * this.s, 0f * this.s, -1f * this.s, +2f * this.s,
            0f * this.s, 0f * this.s, +2f * this.s, 0f * this.s, 1f * this.s,
            +2f * this.s, 0f * this.s, 2f * this.s, +2f * this.s, 0f * this.s,
    };

    // public void draw3DNURBS( GL gl, GLU glu )
    // {
    // int nbSamplePts = 10;
    // gl.glMapGrid2f( nbSamplePts, 0f, 1f,
    // nbSamplePts, 0f, 1f);
    // gl.glEvalMesh2( GL.GL_FILL,
    // 0, nbSamplePts,
    // 0, nbSamplePts );
    // float knots[] = {
    // 0.0, 0.0, 0.0, 0.0,
    // 1.0, 1.0, 1.0, 1.0
    // };
    // //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // // glPushMatrix();
    // // glRotatef(330.0, 1.,0.,0.);
    // // glScalef (0.5, 0.5, 0.5);
    // glu.gluBeginSurface(theNurb);
    // glu.gluNurbsSurface
    // (theNurb,
    // 8, knots,
    // 8, knots,
    // 4 * 3,
    // 3,
    // // &ctlpoints[0][0][0],
    // ctrlPoints,
    // 4, 4,
    // GL_MAP2_VERTEX_3 );
    // glu.gluEndSurface(theNurb);
    // }

    // ////////////////////// NURBS /////////////////////

    // ... one day there will be NURBS !
    // public void initNURBS( GL gl, GLU glu )
    // {
    // //init_surface();
    // //theNurb = glu.gluNewNurbsRenderer();
    // glu.gluNurbsProperty( theNurb,
    // glu.GLU_SAMPLING_TOLERANCE,
    // 25.0);
    // glu.gluNurbsProperty(theNurb, GLU_DISPLAY_MODE, GLU_FILL);
    // }

    // ////////////////////// data /////////////////////

    float ctrlPointsOrg[] = {

            0f, 0f, 0f, 1f, 0f, 0f, 2f, 0f, 0f, 3f, 0f, 0f,

            0f, 0f, 1f, 1f, 1f, 1f, 2f, 1f, 1f, 3f, 0f, 1f,

            0f, 0f, 2f, 1f, 1f, 2f, 2f, 1f, 2f, 3f, 0f, 2f,

            0f, 0f, 3f, 1f, 0f, 3f, 2f, 0f, 3f, 3f, 0f, 3f,
    };

    public Bezier() {}

    public void draw2DBezier(final GL gl) {
        final int nbCtrlPoints = 6;
        final int sizeCtrlPoints = nbCtrlPoints * 3;

        final float a = 1.0f;
        final float b = 0.f;
        final float ctrlPoints[] = {
                0f, 0f, 0f, // ddd
                -a, -a, 0f, -b, +a, 0f, +b, -a, 0f, // ddd
                +a, +a, 0f, +b, -a, 0f

        };

        // Check if the ctrl points array has a legal size.
        if (ctrlPoints.length != sizeCtrlPoints) {
            System.out.println("ERROR ctrlPoints\n");
        };

        gl.glMap1f(GL.GL_MAP1_VERTEX_3, 0.0f, 1.0f, 3, nbCtrlPoints, ctrlPoints, 0);

        gl.glEnable(GL.GL_MAP1_VERTEX_3);

        // Draw ctrlPoints.
        gl.glBegin(GL.GL_POINTS);
        {
            for (int i = 0; i < sizeCtrlPoints; i += 3) {
                gl.glVertex3f(ctrlPoints[i], ctrlPoints[i + 1], ctrlPoints[i + 2]);
            }
        }
        gl.glEnd();

        gl.glMapGrid1f(20, 0f, 1f);
        gl.glEvalMesh1(GL.GL_POINT, 0, 20);
        /*
         * // Draw courve. gl.glBegin(GL.GL_POINTS); { for( float v=0; v <= 1; v
         * += 0.01 ) { gl.glEvalCoord1f( v ); } } gl.glEnd();
         */
    }

    public void drawSpiral(final GL gl) {
        float r = 0.5f;
        final float x, y, z;
        gl.glBegin(GL.GL_POINTS);
        {
            for (float a = 0; a <= 1440; a += 5) {
                r -= 0.5f / 300;
                final float ang = (float) Math.toRadians(a);
                gl.glVertex3f((float) (r * Math.cos(ang)), a / 1440f - 0.5f,
                        (float) (r * Math.sin(ang)));
            }
        }
        gl.glEnd();
    }

}
